using System.Reflection;
using AutoMapper;
using FastDevTemplate.Common.Util;
using FastDevTemplate.Service.DataMapper;
using FastDevTemplate.Service.Interface.Base;

namespace FastDevTemplate.Service.Base;

public class BaseService : IBaseService
{
    protected readonly IMapper ObjectMapper = DtoMapConfig.Config.CreateMapper();

    protected virtual int ErrorHandling(ErrorType errorType)
    {
        // ErrorHandling Logic:
        Console.Error.WriteLine($"[{DateTime.Now}] ErrorType: {errorType}");
        return (int)errorType;
    }

    protected virtual int ErrorHandling(ErrorType errorType, string message)
    {
        // ErrorHandling Logic:
        Console.Error.WriteLine($"[{DateTime.Now}] ErrorType: {errorType} Msg: {message}");
        return (int)errorType;
    }

    protected virtual int ErrorHandling(ErrorType errorType, MethodBase fromMethod)
    {
        // ErrorHandling Logic:
        Console.Error.WriteLine($"[{DateTime.Now}] ErrorType: {errorType} From: {fromMethod.Name}");
        return (int)errorType;
    }

    protected virtual int ErrorHandling(ErrorType errorType, Exception exception)
    {
        // ErrorHandling Logic:
        var lines = exception.StackTrace?.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
        Console.Error.WriteLine(
            $"[{DateTime.Now}] ErrorType: {errorType} , Msg: {exception.Message} , From: {lines?[^1].Trim() ?? ""}");
        return (int)errorType;
    }
}